package com.zxl.message;

import com.alibaba.fastjson.JSONObject;

import com.zxl.model.AccountChangeEvent;
import com.zxl.service.AccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author:  zxl
 * @Date:  2023-03-12
 */
@Component
@Slf4j
@RocketMQMessageListener(consumerGroup = "consumer_group_txmsg_bank2",topic = "topic_txmsg")
public class TxmsgConsumer implements RocketMQListener<String> {
    @Autowired
    AccountInfoService accountInfoService;

    /**
     * description: 接收消息，消息失败会重试
     * @Param  message
     * @Return: void
     */
    @Override
    public void onMessage(String message) {
        log.info("开始消费消息----------------》:{}",message);
        //解析消息
        JSONObject jsonObject = JSONObject.parseObject(message);
        String accountChangeString = jsonObject.getString("accountChange");
        //转成AccountChangeEvent
        AccountChangeEvent accountChangeEvent =
                JSONObject.parseObject(accountChangeString, AccountChangeEvent.class);
        //设置账号为bank2的2号用户的银行卡号
        accountChangeEvent.setAccountNo("2");
        //更新本地账户，增加金额
        accountInfoService.addAccountInfoBalance(accountChangeEvent);
    }
}
